package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dto.ArticlePageDTO;
import com.example.entity.Article;
import com.example.service.ArchiveService;
import com.example.service.ArticleService;
import com.example.vo.ArchiveVO;
import com.example.vo.ArticleArchiveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 归档服务实现类
 */
@Slf4j
@Service
public class ArchiveServiceImpl implements ArchiveService {

    @Resource
    private ArticleService articleService;
    
    // 用于从createTime中提取年月的正则表达式
    private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");

    @Override
    public List<ArchiveVO> getArchiveList(ArticlePageDTO articlePageDTO) {
        // 查询所有文章，不过滤isPublish
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Article::getCreateTime);
        // 暂时移除isPublish过滤
        // wrapper.eq(Article::getIsPublish, true);
        
        List<Article> articles = articleService.list(wrapper);
        
        if (articles.isEmpty()) {
            log.info("归档列表为空，无文章数据");
            return new ArrayList<>();
        }
        
        log.info("查询到文章总数: {}", articles.size());
        
        // 按年月分组
        Map<String, List<Article>> groupByMonth = new HashMap<>();
        
        for (Article article : articles) {
            String createTime = article.getCreateTime();
            if (createTime != null) {
                // 从createTime中提取年月
                Matcher matcher = DATE_PATTERN.matcher(createTime);
                if (matcher.find()) {
                    String yearMonth = matcher.group(1) + "-" + matcher.group(2);
                    log.info("文章ID: {}, 标题: {}, 归入月份: {}", article.getId(), article.getTitle(), yearMonth);
                    groupByMonth.computeIfAbsent(yearMonth, k -> new ArrayList<>()).add(article);
                } else {
                    log.warn("无法解析文章创建时间，文章ID: {}, 创建时间: {}", article.getId(), createTime);
                }
            } else {
                log.warn("文章创建时间为空，文章ID: {}", article.getId());
            }
        }
        
        // 构建归档列表
        List<ArchiveVO> archiveList = new ArrayList<>();
        for (Map.Entry<String, List<Article>> entry : groupByMonth.entrySet()) {
            String month = entry.getKey();
            List<Article> articleList = entry.getValue();
            
            ArchiveVO archiveVO = new ArchiveVO();
            archiveVO.setMonth(month);
            
            // 转换为归档文章VO
            List<ArticleArchiveVO> articleArchiveVOList = new ArrayList<>();
            for (Article article : articleList) {
                ArticleArchiveVO vo = new ArticleArchiveVO();
                vo.setId(article.getId());
                vo.setTitle(article.getTitle());
                
                // 处理封面图
                String titleImage = article.getTitleImage();
                vo.setTitleImage(titleImage);
                
                vo.setCreateTime(article.getCreateTime());
                articleArchiveVOList.add(vo);
            }
            
            archiveVO.setArticles(articleArchiveVOList);
            archiveList.add(archiveVO);
            log.info("月份: {}, 文章数: {}", month, articleArchiveVOList.size());
        }
        
        // 按年月倒序排序
        archiveList.sort((a1, a2) -> a2.getMonth().compareTo(a1.getMonth()));
        
        log.info("归档列表获取成功，共有{}个月份", archiveList.size());
        return archiveList;
    }
} 